package org.eoti.elec;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import org.eoti.elec.db.CircuitDB;
import org.eoti.mimir.MimirException;
import org.eoti.spec.raijin.v1.CircuitConfig;
import org.eoti.spec.raijin.v1.ComponentConfig;
import org.eoti.spec.raijin.v1.PinConfig;
import org.eoti.spec.raijin.v1.WireConfig;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class DigitalCircuit
extends DigitalComponent
{
	protected ConcurrentHashMap<String, DigitalComponent> components;
	protected CopyOnWriteArrayList<Wire> wires;
	protected CircuitDB db;
	protected CircuitConfig config;

	/**
	 * @param db the database
	 * @param config the circuits' configuration
	 * @throws org.eoti.mimir.MimirException if there was a failure loading the circuit
	 */
	public DigitalCircuit(CircuitDB db, CircuitConfig config)
			throws MimirException
	{
		super(config.getName(), config.getSpec().getPropagation());
		components = new ConcurrentHashMap<String, DigitalComponent>();
		wires = new CopyOnWriteArrayList<Wire>();
		this.db = db;
		this.config = config;

		/**
		 * Recursively load the components
		 */
		for( ComponentConfig compConf : config.getComponents())
		{
			String type = compConf.getType();
			if(type.equals(ANDGate.NAME))
			{
				components.put(compConf.getName(), new ANDGate());
			}else if(type.equals(ORGate.NAME)){
				components.put(compConf.getName(), new ORGate());
			}else if(type.equals(NOTGate.NAME)){
				components.put(compConf.getName(), new NOTGate());
			}else{
				CircuitConfig circConf = db.read(compConf.getType());
				components.put(compConf.getName(), new DigitalCircuit(db, circConf));
			}
		}

		/**
		 * Create our pins
		 */
		for( PinConfig pinConf : config.getPins())
			addPin(new Pin(pinConf));

		/**
		 * Create the wiring
		 */
		for( WireConfig wireConf : config.getWires())
			wires.add(new Wire(this, wireConf));
	}

	public String getDescription()
	{
		return config.getSpec().getDescription();
	}

	public float getPowerRequirements()
	{
		return config.getSpec().getPower();
	}

	protected void addComponent(DigitalComponent component)
	{
		components.put(component.getName(), component);
	}

	protected DigitalComponent getComponent(String name)
	{
		return components.get(name);
	}

	public void tick()
	{
		for(Wire wire : wires)
			wire.tick();

		for(DigitalComponent component : components.values())
			component.tick();

		super.tick();
	}
}
